package com.etrans.web.gps.business.report.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.service.AlarmStatisticsService;
import com.etrans.web.gps.business.system.service.CommonService;
import com.etrans.web.gps.business.system.service.ExportWordService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.Config;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

/**
 * 报警综合分析
 * @author jw.chen
 *
 */
@Controller
@RequestMapping("/report/alarmSta/*")
public class AlarmStatisticsController extends BaseController{
	
	private final static Logger logger = LoggerFactory.getLogger(AlarmStatisticsController.class);
	
	@Autowired
	private AlarmStatisticsService alarmStaService;
	
	@Autowired
	public CommonService commonService;
	
	@Autowired
	private ExportWordService exportWord;
	
	@Autowired
	private Config config;
	/**
	 * 页面跳转入口
	 * @return
	 */
	@RequestMapping(value="getAlarmStatistics.html")
	public ModelAndView getAlarmSynAnalysis(){
		ModelAndView mv =new ModelAndView();
		mv.setViewName("views/business/report/alarmStatistics");
		return mv;
	}
	
	/**
	 * 点击柱状图关联折线图数据
	 * @return 
	 */
	@ResponseBody
	@RequestMapping(value = "loadTableData.html")
	public PageVo loadTableData(String paramsGrid){
		PageVo pageVo = new PageVo();
		try {
			//获取查询条件
			HashMap<String, Object> params = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
			int page = Integer.parseInt(String.valueOf(params.get("page")));
			int pageSize = Integer.parseInt(String.valueOf(params.get("pageSize")));
			String queryType = params.get("queryType") == null? "": String.valueOf(params.get("queryType"));
			String timeType = params.get("timeType") == null? "": String.valueOf(params.get("timeType"));
			String queryTime = params.get("queryTime") == null? "": String.valueOf(params.get("queryTime"));
			String queryArea = params.get("queryArea") == null? "": String.valueOf(params.get("queryArea"));
			String alarmType = params.get("alarmType") == null? "": String.valueOf(params.get("alarmType"));
			//查询该权限下的区域
			int area_id= this.getSysUser().getArea_id();
			//区域限制
			List<Integer> areaIdList = new ArrayList<>();
			if(queryArea != null && !"".equals(queryArea)){
				params.put("level", 3);
				params.put("areaIds", queryArea);
				List<HashMap<String, Object>> listAreaIds = commonService.findAreaIdByLevelAndIds(params);
				for (HashMap<String, Object> hashMap : listAreaIds) {
					areaIdList.add(Integer.parseInt(hashMap.get("ID")+""));
				}
			}else {
				areaIdList =commonService.queryAreaListById(area_id);
			}
			if("0".equals(queryType)){
				pageVo = alarmStaService.getEnterpriseData(alarmType,timeType,queryTime,areaIdList,page,pageSize);
			}else if("1".equals(queryType)){
				pageVo = alarmStaService.getVehicleData(alarmType,timeType,queryTime,areaIdList,page,pageSize);
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return pageVo;
	}
	
	/**
	 * 
	 * @param response
	 * @param request
	 * @param timeType
	 * @param queryTime
	 * @param queryArea
	 */
	@RequestMapping(value = "exportTableData.html")
	public void exportTableData(HttpServletResponse response,HttpServletRequest request,String timeType, 
			String queryTime, String queryArea){
		try {
			String fileName = "";
			String vm_name = "";
			if (queryArea == null || !StringUtils.isNumeric(queryArea)) {
				return;
			}
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
			int areaId = Integer.valueOf(queryArea);
			Map<String, Object> dataMap = new HashMap<>();
			String area = alarmStaService.queryAreaNameById(areaId);
			dataMap.put("vechicleRegister", area);
			if ("week".equals(timeType) && queryTime.length() == 7) {
				//标题及概述
				String[] tStr = (queryTime).split("-");
				String weekStr = tStr[0] + "年第" + tStr[1] + "周";
				dataMap.put("week", weekStr);
				dataMap.put("platName", new String(config.plat_name.getBytes("iso-8859-1") , "UTF-8"));
				fileName = area + weekStr;
				Map<String,Object> map =DateUtil.getYearAndWeekByDate(Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
				Date startDate = DateUtil.getStrToDate((String)map.get("Monday"));
				Date endDate = DateUtil.getStrToDate((String)map.get("Sunday"));
				dataMap.put("startDate", sdf.format(startDate));
				dataMap.put("endDate", sdf.format(endDate));
				String query_id = new SimpleDateFormat("yyMMdd").format(DateUtil.getStrToDate((String)map.get("Monday")));
				//报表1(车辆超速排名) report_1
				List<Map<String, String>> report_1 = alarmStaService.exportVehicleDataList("1","vehicle_swap_week",query_id,areaId);
				dataMap.put("report_1", report_1);
				//报表2(企业超速排名) report_2
				List<Map<String,String>> report_2 = alarmStaService.exportEnterpriseDataList("1","enterprise_swap_week",query_id,areaId);
				dataMap.put("report_2", report_2);
				//报表3(超速行驶-车速≧100km/h) report_3
				List<Map<String,String>> report_3 = alarmStaService.getVehicleOverSpeedList(startDate,endDate,areaId);
				dataMap.put("report_3", report_3);
				//报表4(疲劳驾驶车辆排名) report_4
				List<Map<String,String>> report_4 = alarmStaService.exportVehicleDataList("2","vehicle_swap_week",query_id,areaId);
				dataMap.put("report_4", report_4);
				//报表5(疲劳驾驶企业排名) report_5
				List<Map<String,String>> report_5 = alarmStaService.exportEnterpriseDataList("2","enterprise_swap_week",query_id,areaId);
				dataMap.put("report_5", report_5);
				//报表6(离线报警车辆排名) report_6
				List<Map<String,String>> report_6 = alarmStaService.exportVehicleDataList("3","vehicle_swap_week",query_id,areaId);
				dataMap.put("report_6", report_6);
				//报表7(离线报警企业排名) report_7
				List<Map<String,String>> report_7 = alarmStaService.exportEnterpriseDataList("3","enterprise_swap_week",query_id,areaId);
				dataMap.put("report_7", report_7);
				//报表8(2-5点禁行车辆排名) report_8
				List<Map<String,String>> report_8 = alarmStaService.exportVehicleDataList("4","vehicle_swap_week",query_id,areaId);
				dataMap.put("report_8", report_8);
				//报表9(2-5点禁行企业排名) report_9
				List<Map<String,String>> report_9 = alarmStaService.exportEnterpriseDataList("4","enterprise_swap_week",query_id,areaId);
				dataMap.put("report_9", report_9);
				//报表10(2-5点禁行车辆排名) report_10
				//List<Map<String,String>> report_10 = alarmStaService.exportVehicleDataList("5","vehicle_swap_week",query_id,areaId);
				//dataMap.put("report_10", report_10);
				//报表11(2-5点禁行企业排名) report_11
				//List<Map<String,String>> report_11 = alarmStaService.exportEnterpriseDataList("5","enterprise_swap_week",query_id,areaId);
				//dataMap.put("report_11", report_11);
				vm_name = "NetworkSpreadingWeek.xml";
			}else if ("month".equals(timeType) && queryTime.length() == 7) {
				fileName = "";
				vm_name = "NetworkSpreadingMonth.xml";
			}else {
				return;
			}
			
			exportWord.exportWord(response,dataMap,fileName,vm_name);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
	}
}
